<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Sms extends MY_Controller {

	public function __construct(){
		parent::__construct();
		$this->load->model('sms_m');
		ini_set('max_execution_time', '70');
	}


	/*-检查是否有新短信-*/
	//PHP脚本超时事件设为70秒，循环检查间隔3秒，一次循环检查最长事件60秒返回空条数后等待重新发起长链接
	function checkSms(){	
		$response = array();	
		//$response['timestamp'] = time();
		$response['msg'] = '';
		
		$newsms = 0;//新短信条数判断变量
		$u_timing = 0;//长连接计时变量
		//$newsms = $this->sms_m->checknew();
		//无限循环，直到数据文件没有被修改
		do{
			++$u_timing;
			sleep(3); // 睡眠3秒
			$newsms = $this->sms_m->checknew();

			if($newsms>0){			
				$response['msg'] = $this->sms_m->showSms();// 返回一个JSON数组
			}
		}while($newsms == 0 && $u_timing < 20); // 检查数据文件已经被修改
		echo json_encode($response);
		flush();//刷新缓冲区的内容，输出

	}

	
	/*--写短信------------------------------------------------------------------------------*/
	public function sms_add(){//新短信
		$this->load->view('sms/new');
	}
	
	/*--写短信POST提交处理------------------------------------------------------------------------------*/
	public function _sms_add_post(){//新邮件
		$arrdata=$this->input->post();
		$data = $this->sms_m->smsaddpost($arrdata);
		$this->rejsonsuccess($data,"发送邮件失败，请联系管理员");	
	}
	
	
	//已收短信
	public function inbox(){//已收短信视图
		$this->load->view('sms/inbox');
	}
	
	//已收短信JSON数据
	public function _inbox_data_post(){
		if($this->acl->check_privilege('sms_inbox') == false){
			show_error("您没有权限访问此页面");
			return ;
		}else{
			$rowdata = '';
			$data = $this->sms_m->getinboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['smsid'],"sender"=>$v['sender'],"title"=>$v['content'],"sendertime"=>$v['sendtime'],"smstype"=>$v['smstype'],"bodyid"=>$v['bodyid']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}
	
	//重新提醒
	public function _remind_reset_post(){
		$data=$this->input->post('data',TRUE);
		foreach ($data as $v){
			//$this->db->where('smsid', $v['id'])->update('sms', array('sms_read' => 0));
			$smsdata[] = array('smsid' => $v['id'],'sms_read' => 0);
			$sms_checkdata[] = array(//用于检索的数据表数据
				'id' => '',
				'u_id' => $v['to_id'],
				'u_name' => $v['to_name'],
				'add_date' => time(),
				'm_id' => $v['smstype'].'_'.$v['bodyid'],
				'check_read' => 0
			);
		}
		$a = $this->db->update_batch('sms',$smsdata, 'smsid'); 
		$a = $this->db->insert_batch('sms_check',$sms_checkdata); //插入接收邮件数组
		$this->rejsonsuccess($a,"'标记为已读'失败，请联系管理员");	
	}
	public function _inbox_del_post(){//已收短信删除
		$id=$this->input->post();
		$data = $this->sms_m->inboxdel($id['getid']);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}

	
	/*----已发短信-------------------------------------------------------------------*/
	public function sentbox(){//已发短信视图
		$this->load->view('sms/sentbox');
	}
	
	//已收短信JSON数据
	public function _sentbox_data_post(){
		if($this->acl->check_privilege('sms_sentbox') == false){
			show_error("you have no privilege to access this page");
			return ;
		}else{
			$rowdata = '';
			$data = $this->sms_m->getsentboxlistdata($this->input->post());
			if(!empty($data['data'])){
				foreach ($data['data'] as $v){
					$rowdata[] = array("id"=>$v['bodyid'],"to_id"=>$v['to_id'],"to_name"=>$v['to_name'],"title"=>$v['content'],"readtime"=>$v['readtime'],"smstype"=>$v['smstype'],"bodyid"=>$v['bodyid']);
				}
			}
			$data = array("total"=>$data['total'],'rows'=>$rowdata);
			echo json_encode($data); 
		}
	}
	
	//发件箱删除
	public function _sentbox_del_post(){
		$id=$this->input->post();//当前页数
		$data = $this->sms_m->sentboxdel($id['getid']);
		$this->rejsonsuccess($data,"'标记为已读'失败，请联系管理员");		
	}
	
	//已收短信中的搜索功能视图页
	function advanced_search(){
		$m = $this->uri->segment(3, 0);
		if($m == 'sms_in'){
			$t = '已收短信';
		}else if($m == 'sms_sent'){
			$t = '已发短信';	
		}
		$data['v'] = array('m'=>$m,'t'=>$t);
		$this->load->view('sms/sms_search',$data);
	}
	
}
?>